package dyy.zc.campuspay.ui.fragment;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.io.File;

import dyy.zc.campuspay.R;
import dyy.zc.campuspay.base.BaseFragment;
import dyy.zc.campuspay.ui.activity.ScannerActivity;
import dyy.zc.campuspay.ui.activity.ZhuanZhangActivity;
import dyy.zc.campuspay.utils.Utils;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by yyuand on 2017.3.14.
 */

public class CodeFragment extends BaseFragment implements View.OnClickListener {
    private TextView tvTitle;
    private ImageView iv_code;
    private ImageView scanner;
    @Override
    protected View onCreateViews(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_code,container,false);
        initView(view);
        initData();
        bind();
        return view;
    }

    private void initView(View view) {
        tvTitle = Utils.findViewsById(view,R.id.top_tv_title);
        iv_code= Utils.findViewsById(view,R.id.iv_code);
        scanner = Utils.findViewsById(view,R.id.iv_scanner);

    }

    private void initData() {
        tvTitle.setText("我的二维码");
        Bitmap bitmap = CodeUtils.createImage(loginuser.getId(), 250, 250,null);
        iv_code.setImageBitmap(bitmap);
    }

    private void bind() {
        scanner.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_scanner:
                if (EasyPermissions.hasPermissions(getActivity(), Manifest.permission.CAMERA)){
                    Intent intent = new Intent(getActivity(), ScannerActivity.class);
                    startActivityForResult(intent,456);
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 456:
                if (data != null){
                    Bundle bundle = data.getExtras();
                    String id = bundle.getString(CodeUtils.RESULT_STRING);
                    Intent intent = new Intent(getActivity(),ZhuanZhangActivity.class);
                    intent.putExtra("id",id);
                    startActivity(intent);
                }
                //startActivity(new Intent(getActivity(), ZhuanZhangActivity.class));
                break;
        }
    }
}
